package session.stateful.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.ejb.Stateful;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import exception.UnknownAccountException;

import model.Account;
import session.stateful.UserBeanRemote;

/**
 * Session Bean implementation class UserBean
 */
@Stateful
public class UserBean implements UserBeanRemote {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private InitialContext jndiContext = null;
	private Connection connection = null;
	private DataSource datasource = null;

	/**
     * Default constructor. 
     */
    public UserBean() {
        // TODO Auto-generated constructor stub
    	init();
    }

	@Override
	public Account find(String id, String password) throws UnknownAccountException {
		// TODO Auto-generated method stub
		Connection con=getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		String i = null;
		String p = null;
		float b = 0;
		Account account = null;

		try {	
			stmt=con.prepareStatement("select * from user where id = ? and password = ?");
			stmt.setString(1, id);
			stmt.setString(2, password);
			result=stmt.executeQuery();
			if (result.next()) {
				i = result.getString(1);
				p = result.getString(2);
				b = result.getFloat(3);
				account = new Account(i, p, b);
			} else {
				throw new UnknownAccountException();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closeConnection(con);
			closePreparedStatement(stmt);
			closeResult(result);
		}
		return account;
	}

	private void init() {
		Properties properties = new Properties();
		properties.put(javax.naming.Context.PROVIDER_URL, "jnp:///");
		properties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");

		try {
			jndiContext = new InitialContext(properties);
			datasource = (DataSource) jndiContext.lookup("java:comp/env/jdbc/bankaccountEJB");
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private Connection getConnection() {

		try {
			connection = datasource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return connection;
	}

	private void closeConnection(Connection con) {
		if(con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void closePreparedStatement(PreparedStatement stmt) {
		if(stmt!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void closeResult(ResultSet result) {
		if(result!=null) {
			try {
				result.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
